package com.bdqn.dao;

import java.sql.*;

public class TestJdbc {//main+回车
    public static void main(String[] args){
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;
        //第一步加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接对象
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?serverTimezone=GMT-8", "root", "123");
            //写SQL语句
            String sql="SELECT studentname,phone,address FROM student";
            //执行SQL
             stmt = con.createStatement();
             rs = stmt.executeQuery(sql);
            //处理结果集  System.out.println()快捷方法sout+回车
            while (rs.next()){
                String name=rs.getString(1);
                String phone=rs.getString(2);
                String address=rs.getString(3);
                System.out.println(name+"\t"+phone+"\t"+address);
            //释放资源
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
      if (con!=null){
          try {
              con.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
        }
    }
}
